package com.coffee.service.impl;

import com.coffee.common.BaseContext;
import com.coffee.common.CustomException;
import com.coffee.dao.OrderDAO;
import com.coffee.entity.*;
import com.coffee.service.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderDAO orderDAO;

    /**
     * 生成订单
     *
     * @param orders
     */
    @Override
    @Transactional
    public void submit(Orders orders) {
        //获取当前用户id
        String userId = BaseContext.get();
        //查询当前用户的购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(userId);
        if (shoppingCartList == null || shoppingCartList.size() == 0) {
            throw new CustomException("购物车为空无法下单");
        }
        //查询用户数据
        User user = userService.getById(userId);
        //组装订单明细，并给订单明细表中的主表id赋值，使其与主表产生关系
        //遍历购物车集合
        //订单明细集合
        //主表id
        String orderId = UUID.randomUUID().toString();//订单号
        ArrayList<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart,orderDetail);
            String orderDetailId = UUID.randomUUID().toString();
            orderDetail.setOrderId(orderId);
            orderDetail.setId(orderDetailId);
            orderDetailList.add(orderDetail);
        }
        Orders orderMain = new Orders();
        //主键id
        orderMain.setId(orderId);
        //订单号,往往是有意义的
        orderMain.setNumber(orderId+"");
        //订单状态
        orderMain.setStatus(2);
        //当前用户ID
        orderMain.setUserId(BaseContext.get());
        //订单派送地址
        orderMain.setAddress(orders.getAddress());
        //订单时间
        orderMain.setOrderTime(LocalDateTime.now());
        //结账时间
        orderMain.setCheckoutTime(LocalDateTime.now());
        //支付方式
        orderMain.setPayMethod(orders.getPayMethod());
        //订单的总价值
        orderMain.setAmount(orders.getAmount());
        //备注
        orderMain.setRemark(orders.getRemark());
        //用户名
        orderMain.setUserName(user.getName());
        //收货人手机号
        orderMain.setPhone(orders.getPhone());
        //收货人
        orderMain.setConsignee(orders.getConsignee());
        //6.保存订单
        this.save(orderMain);
        //7.保存订单明细
        this.orderDetailService.saveBatch(orderDetailList);
        //8.删除购物车数据
        this.shoppingCartService.remove(userId);
    }

    /**
     * 取消订单
     * @param userId
     */
    @Override
    public void remove(String orderId,String userId) {
        //删除订单
        orderDAO.remove(orderId,userId);
       //删除订单明细
        orderDAO.removeDetail(orderId);
    }

    /**
     * 查询用户订单
     * @param userId
     * @return
     */
    @Override
    public List<Orders> listOrd(String userId) {
        return orderDAO.listOrd(userId);
    }

    /**
     * 查询订单信息
     * @param orderId
     * @return
     */
    @Override
    public List<OrderDetail> listDet(String orderId) {
       return orderDAO.listDet(orderId);
    }
    /**
     * 订单管理分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询page
        Page<Orders> page= orderDAO.select(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 保存订单
     * @param orderMain
     */
    private void save(Orders orderMain) {
        orderDAO.save(orderMain);
    }

    /**
     * pc端批量删除订单记录
     * @param ids
     */
    @Override
    public void removeBatch(List<String> ids) {
        orderDAO.deletebatch(ids);

    }

    @Override
    public HashMap<String, Integer> getStatistics() {
        //从订单明细查询咖啡销售总数量
        return  orderDAO.getSum();
    }

    @Override
    public void status(Orders orders) {
        orderDAO.status(orders);
    }

    @Override
    public HashMap<String, Integer> getAnalyse() {
        //从订单根据时间查询咖啡数量
        return  orderDAO.getByTime();
    }
}
